///**
// * @Company: 北京慧点科技有限公司 | www.smartdot.com.cn
// * @Copyright 1998-2022 © Smartdot Technologies Co., Ltd.
// */
//
//package com.hd.rcugrc.project.leaveapplication.leave.service.impl;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
//import com.hd.rcugrc.project.leaveapplication.leave.service.WeFlowListService;
//import com.hd.rcugrc.project.leaveapplication.leave.util.flowListUtil;
//import com.hd.wep.common.bean.WeActiveStepInfo;
//import com.hd.wep.common.service.impl.WeFlowOperaServiceImpl;
//import com.hd.wep.extend.common.service.WeListService;
//import org.apache.commons.lang3.StringUtils;
//
//import com.hd.rcugrc.bpm.User;
//import com.hd.rcugrc.bpm.facade.HdProcessLogInfo;
//import com.hd.rcugrc.bpm.facade.mobile.mvc.query.ListQueryResult;
//import com.hd.rcugrc.bpm.facade.service.ProcessTracingService;
//import com.hd.rcugrc.data.crud.criteria.Comparison;
//import com.hd.rcugrc.data.crud.criteria.Criterion;
//import com.hd.rcugrc.data.crud.criteria.Operator;
//import com.hd.rcugrc.data.crud.criteria.OrderBy;
//import com.hd.rcugrc.form.model.EntityResourceDefinition;
//import com.hd.rcugrc.todolist.TodoListItem;
//import com.hd.rcugrc.todolist.TodoListService;
//import com.hd.wep.extend.model.service.WeModelDesignFieldService;
//import com.hd.wep.common.utils.DateUtils;
//
//
///**
// * @author smartdot
// * @version 1.0 - 2024-07-15 09:35:47
// */
//public class WeFlowListServiceImpl implements WeFlowListService {
//
//    private WeModelDesignFieldService weModelDesignFieldService;
//    private WeListService weListService;
//    private TodoListService todoListService;
//    private WeFlowOperaServiceImpl weFlowOperaServiceImpl;
//    private ProcessTracingService processTracingService;
//
//    public WeModelDesignFieldService getWeModelDesignFieldService() {
//        return weModelDesignFieldService;
//    }
//
//    public void setWeModelDesignFieldService(WeModelDesignFieldService weModelDesignFieldService) {
//        this.weModelDesignFieldService = weModelDesignFieldService;
//    }
//
//    public WeListService getWeListService() {
//        return weListService;
//    }
//
//    public void setWeListService(WeListService weListService) {
//        this.weListService = weListService;
//    }
//
//    public TodoListService getTodoListService() {
//        return todoListService;
//    }
//
//    public void setTodoListService(TodoListService todoListService) {
//        this.todoListService = todoListService;
//    }
//
//    public WeFlowOperaServiceImpl getWeFlowOperaServiceImpl() {
//        return weFlowOperaServiceImpl;
//    }
//
//    public void setWeFlowOperaServiceImpl(WeFlowOperaServiceImpl weFlowOperaServiceImpl) {
//        this.weFlowOperaServiceImpl = weFlowOperaServiceImpl;
//    }
//
//    public ProcessTracingService getProcessTracingService() {
//        return processTracingService;
//    }
//
//    public void setProcessTracingService(ProcessTracingService processTracingService) {
//        this.processTracingService = processTracingService;
//    }
//
//    @Override
//    public ListQueryResult findActiveList(User owner, String formId, String[] columnarray, Criterion[] criterions,
//            int PageIndex, int PageSize, OrderBy[] orderBy, boolean authCodeFlag, String authCodeRoles) {
//        if (StringUtils.isBlank(formId)) {
//            throw new IllegalArgumentException("表单参数异常：{formId}" + formId);
//        }
//        EntityResourceDefinition entity = weModelDesignFieldService.getEntityInfo(formId);
//        // 获取列表数据总数
//        int count = this.weListService.findActiveEntryCount(owner, entity, formId, authCodeFlag, authCodeRoles,
//                criterions);
//        ListQueryResult result = new ListQueryResult();
//        if (count == 0) {
//            result.setData(new Object[0]);
//            result.setTotalSize(count);
//            return result;
//        }
//
//        List<Map<String, String>> activeMapList = this.weListService.findActiveMap(owner, entity, columnarray, orderBy,
//                PageIndex, PageSize, authCodeFlag, authCodeRoles, criterions);
//        setFlowInstanceId(activeMapList);
//        // 加入待办id
//        setTodoId(activeMapList, owner);
//        setTimeByZone(activeMapList, flowListUtil.TAB_ACTIVE);
//        result.setData(activeMapList.toArray());
//        result.setTotalSize(count);
//        return result;
//    }
//
//    @Override
//    public ListQueryResult findProcessedList(User owner, String formId, String[] columnarray, Criterion[] criterions,
//            int PageIndex, int PageSize, OrderBy[] orderBy, boolean authCodeFlag, String authCodeRoles) {
//        if (StringUtils.isBlank(formId)) {
//            throw new IllegalArgumentException("表单参数异常：{formId}" + formId);
//        }
//        EntityResourceDefinition entity = weModelDesignFieldService.getEntityInfo(formId);
//        // 获取列表数据总数
//        int count = this.weListService.findProcessedEntryCount(owner, entity, formId, authCodeFlag, authCodeRoles,
//                criterions);
//        ListQueryResult result = new ListQueryResult();
//        if (count == 0) {
//            result.setData(new Object[0]);
//            result.setTotalSize(count);
//            return result;
//        }
//
//        List<Map<String, String>> processedMapList = this.weListService.findProcessedMap(owner, entity, columnarray,
//                orderBy, PageIndex, PageSize, authCodeFlag, authCodeRoles, criterions);
//        setFlowInstanceId(processedMapList);
//        // 设置当前环节
//        setCurrentStep(processedMapList);
//        setTimeByZone(processedMapList, flowListUtil.TAB_PROCESSED);
//        result.setData(processedMapList.toArray());
//        result.setTotalSize(count);
//        return result;
//    }
//
//    @Override
//    public ListQueryResult findCompletedList(User owner, String formId, String[] columnarray, Criterion[] criterions,
//            int PageIndex, int PageSize, OrderBy[] orderBy, boolean authCodeFlag, String authCodeRoles) {
//        if (StringUtils.isBlank(formId)) {
//            throw new IllegalArgumentException("表单参数异常：{formId}" + formId);
//        }
//        EntityResourceDefinition entity = weModelDesignFieldService.getEntityInfo(formId);
//        // 获取列表数据总数
//        int count = this.weListService.findCompletedEntryCount(owner, entity, formId, authCodeFlag, authCodeRoles,
//                criterions);
//        ListQueryResult result = new ListQueryResult();
//        if (count == 0) {
//            result.setData(new Object[0]);
//            result.setTotalSize(count);
//            return result;
//        }
//
//        List<Map<String, String>> completedMapList = this.weListService.findCompletedMap(owner, entity, columnarray,
//                orderBy, PageIndex, PageSize, authCodeFlag, authCodeRoles, criterions);
//        setFlowInstanceId(completedMapList);
//        setTimeByZone(completedMapList, flowListUtil.TAB_COMPLETED);
//        result.setData(completedMapList.toArray());
//        result.setTotalSize(count);
//        return result;
//    }
//
//    /**
//     * 同步时区
//     * @param list
//     * @param tabType
//     */
//    private void setTimeByZone(List<Map<String, String>> list, String tabType) {
//        if (list.isEmpty()) {
//            return;
//        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        for (Map<String, String> map : list) {
//            if (flowListUtil.TAB_ACTIVE.equals(tabType)) {
//                Object timeObject = map.get("STARTDATE");
//                if (null != timeObject) {
//                    Date receiveTime = (Date) timeObject;
//                    Date date = new Date(DateUtils.getTimeByZone(receiveTime));
//                    map.put("STARTDATE", sdf.format(date));
//                }
//            } else if (flowListUtil.TAB_PROCESSED.equals(tabType)) {
//                Object timeObject = map.get("PROCESSED_FINISHDATE");
//                if (null != timeObject) {
//                    Date processedFinishDate = (Date) timeObject;
//                    Date date = new Date(DateUtils.getTimeByZone(processedFinishDate));
//                    map.put("PROCESSED_FINISHDATE", sdf.format(date));
//                }
//
//            } else if (flowListUtil.TAB_COMPLETED.equals(tabType)) {
//
//                Object timeObject = map.get("FINISHDATE");
//                if (null != timeObject) {
//                    Date finishDate = (Date) timeObject;
//                    Date date = new Date(DateUtils.getTimeByZone(finishDate));
//                    map.put("FINISHDATE", sdf.format(date));
//                }
//            }
//        }
//    }
//
//    /**
//     * 设置当前环节
//     *
//     * @param list
//     */
//    private void setCurrentStep(List<Map<String, String>> list) {
//        if (list.isEmpty()) {
//            return;
//        }
//        for (Map<String, String> map : list) {
//            // 设置当前状态（环节）和操作人
//            String flowId = map.get("FLOWID");
//            Object processInstIdObject = map.get("PROCESSED_INSTID");
//            Number instId = (Number) processInstIdObject;
//            WeActiveStepInfo flowActiveStepInfo = weFlowOperaServiceImpl.getFlowActiveStepInfo(flowId,(long) instId);
//            if (flowActiveStepInfo != null) {
//                map.put("CURRENT_USER", flowActiveStepInfo.getOperatorNames());
//                if (null != flowActiveStepInfo.getStepName()) {
//                    map.put("CURRENT_STATUS", flowActiveStepInfo.getStepName());
//                } else {
//                    // 当前流程流转记录
//                    HdProcessLogInfo[] logs = processTracingService.getProcessLogs(instId.longValue());
//                    if (null != logs && logs.length > 0) {
//                        map.put("CURRENT_STATUS", logs[logs.length - 1].getStepType());
//                    }
//                }
//            }
//        }
//    }
//
//    /**
//     * 返回的列表数据增加流程引用表单key,用于模板页面接收
//     *
//     * @param list
//     */
//    private void setFlowInstanceId(List<Map<String, String>> list) {
//        if (list.isEmpty()) {
//            return;
//        }
//        Object instId = "";
//        for (Map<String, String> map : list) {
//            instId = map.get("ID");
//            if (null != instId && !"".equals(instId)) {
//                map.put("FLOWINSTANCEID", String.valueOf(instId));
//            }
//        }
//    }
//
//    /**
//     * 加入待办id
//     *
//     * @param list
//     * @param owner
//     */
//    private void setTodoId(List<Map<String, String>> list, User owner) {
//        if (list.isEmpty()) {
//            return;
//        }
//
//        for (Map<String, String> map : list) {
//            Object instId = map.get("ID");
//            Object stepId = map.get("CURRENTSTEPID");
//            if (null != instId && null != stepId) {
//                Criterion criterionOne = new Comparison("INSTID", Operator.EQ, String.valueOf(instId));
//                Criterion criterionTwo = new Comparison("STEPID", Operator.EQ, String.valueOf(stepId));
//                Criterion[] criterions = new Criterion[2];
//                criterions[0] = criterionOne;
//                criterions[1] = criterionTwo;
//                TodoListItem[] todoList = todoListService.findTodoListItem(null, null, 0, -1, criterions);
//                if (todoList != null && todoList.length > 0) {
//                    for (TodoListItem item : todoList) {
//                        if (owner.getAccount().equals(item.getOwner())) {
//                            map.put("TODO_ID", String.valueOf(item.getId()));
//                            break;
//                        }
//                    }
//                }
//            }
//        }
//    }
//}
